package com.server.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.ICheckService;
import com.server.service.ICommonDataService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("CheckService")
public class CheckService implements ICheckService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	 /**
	 * 课程审核列表
	 * @param operator
	 * @param state
	 * @param requestInput
	 * @return
	 * @author ServerZhang
	 * @date 2016年8月1日
	 */
	@Override
	public DataVector<Map<String, Object>> courseList (String operator, 
			Integer state, RequestInput requestInput){
		String table = "education.course";
		String _whereStr = "id in (select relaId from education.audit where type=1";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public Boolean add(Integer relaId,Integer type,Integer state,
			String content,String operater){
		if (relaId == null || null == type || StringUtils.isEmpty(operater)
				|| state == null) {
			return false;
		}
		Boolean isSuccess = true;
		String sql = "update education.audit set auditor='%s',auditTime='%s',auditOpinion='%s',state=%s where relaId=%s and type=%s";
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		String format = String.format(sql, operater,dateStr,content,state,relaId,type);
		try {
			int chageCount = jdbcTemplate.update(format);
			if (chageCount < 1) {
				isSuccess = false;
			}
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public DataVector<Map<String, Object>> caseList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.case_warehouse";
		String _whereStr = "id in (select relaId from education.audit where type=3";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> examList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.exam";
		String _whereStr = "id in (select relaId from education.audit where type=2";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> surveyList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.survey";
		String _whereStr = "id in (select relaId from education.audit where type=4";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> sicfConList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.sicfa_con_reg_sit";
		String _whereStr = "id in (select relaId from education.audit where type=5";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> sicfEmrList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.sicfa_emr_hand";
		String _whereStr = "id in (select relaId from education.audit where type=7";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> sicfForList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.sicfa_for_ins_per";
		String _whereStr = "id in (select relaId from education.audit where type=6";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public DataVector<Map<String, Object>> sicfRelList(String operator,
			Integer state, RequestInput requestInput) {
		String table = "education.sicfa_rel_sub";
		String _whereStr = "id in (select relaId from education.audit where type=8";
		switch (state) {
		case 0://待审核 + 我审核的课程
			_whereStr += ")";
			break;
		case 1://待审核
			_whereStr += " and auditTime is null) and state=3";
			break;
		case 2://审核过的课程
			_whereStr += " and auditTime is not null and)";
			break;

		default:
			break;
		}
		String sortStr = "createTime desc";
		return commonDataService.getListData(table, _whereStr, sortStr, requestInput);
	}
	@Override
	public List<Map<String, Object>> getAudit(String operator, Integer id,
			Integer typeId) {
		String sql = "select * from education.audit where relaId=%d and type=%d";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql,id,typeId ));
		if(list.size()>0){
			return list;
		}
		return null;
	}
	
}
